import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/layouts/MainLayout.vue'
import OrganizationOnboarding from '@/views/onboarding/OrganizationOnboarding.vue'
import OrganizationSelector from '@/views/onboarding/OrganizationSelector.vue'

const routes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录' }
  },
  
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/Index.vue'),
        meta: { title: '首页', icon: 'Home' }
      }
    ]
  },
  
  // 多组织管理
  {
    path: '/multi-org',
    component: Layout,
    meta: { title: '多组织管理', icon: 'OfficeBuilding' },
    children: [
      {
        path: 'organization-onboarding',
        name: 'OrganizationOnboarding',
        component: OrganizationOnboarding,
        meta: { title: '组织向导', icon: 'MagicStaff' }
      },
      {
        path: 'organization-selector',
        name: 'OrganizationSelector',
        component: OrganizationSelector,
        meta: { title: '组织选择', icon: 'Select' }
      },
      {
        path: 'multi-org-dashboard',
        name: 'MultiOrgDashboard',
        component: () => import('@/views/analysis/CrossOrganization.vue'),
        meta: { title: '多组织分析', icon: 'DataLine' }
      }
    ]
  },
  
  // 数据管理
  {
    path: '/data',
    component: Layout,
    meta: { title: '数据管理', icon: 'Data' },
    children: [
      {
        path: 'plugin-management',
        name: 'PluginDataManagement',
        component: () => import('@/views/data/PluginDataManagement.vue'),
        meta: { title: '插件数据管理' }
      }
    ]
  },
  
  // 财务管理
  {
    path: '/finance',
    component: Layout,
    meta: { title: '财务管理', icon: 'Money' },
    children: [
      // 银行流水
      {
        path: 'transactions',
        name: 'FinanceTransactions',
        component: () => import('@/views/finance/BankTransactions.vue'),
        meta: { title: '银行流水' }
      },
      // 催款管理
      {
        path: 'collection',
        name: 'FinanceCollection',
        component: () => import('@/views/finance/CollectionManagement.vue'),
        meta: { title: '催款管理' }
      },
      // 进项发票
      {
        path: 'invoices/incoming',
        name: 'IncomingInvoices',
        component: () => import('@/views/finance/invoices/IncomingInvoices.vue'),
        meta: { title: '进项发票' }
      },
      // 进项发票新增
      {
        path: 'invoices/incoming/add',
        name: 'IncomingInvoiceAdd',
        component: () => import('@/views/finance/invoices/IncomingInvoiceEdit.vue'),
        meta: { title: '新增进项发票' }
      },
      // 进项发票编辑
      {
        path: 'invoices/incoming/edit/:id',
        name: 'IncomingInvoiceEdit',
        component: () => import('@/views/finance/invoices/IncomingInvoiceEdit.vue'),
        meta: { title: '编辑进项发票' }
      },
      // 进项发票详情
      {
        path: 'invoices/incoming/view/:id',
        name: 'IncomingInvoiceView',
        component: () => import('@/views/finance/invoices/IncomingInvoiceDetail.vue'),
        meta: { title: '查看进项发票' }
      },
      // 销项发票
      {
        path: 'invoices/outgoing',
        name: 'OutgoingInvoices',
        component: () => import('@/views/finance/invoices/OutgoingInvoices.vue'),
        meta: { title: '销项发票' }
      },
      // 销项发票新增
      {
        path: 'invoices/outgoing/add',
        name: 'OutgoingInvoiceAdd',
        component: () => import('@/views/finance/invoices/OutgoingInvoiceEdit.vue'),
        meta: { title: '新增销项发票' }
      },
      // 销项发票编辑
      {
        path: 'invoices/outgoing/edit/:id',
        name: 'OutgoingInvoiceEdit',
        component: () => import('@/views/finance/invoices/OutgoingInvoiceEdit.vue'),
        meta: { title: '编辑销项发票' }
      },
      // 销项发票详情
      {
        path: 'invoices/outgoing/view/:id',
        name: 'OutgoingInvoiceView',
        component: () => import('@/views/finance/invoices/OutgoingInvoiceDetail.vue'),
        meta: { title: '查看销项发票' }
      }
    ]
  },
  
  // 智能对账
  {
    path: '/finance/reconciliation',
    component: Layout,
    meta: { title: '智能对账', icon: 'Coin' },
    children: [
      {
        path: 'workbench',
        name: 'ReconciliationWorkbench',
        component: () => import('@/views/reconciliation/SmartWorkbench.vue'),
        meta: { title: '智能对账工作台' }
      },
      {
        path: 'results',
        name: 'ReconciliationResults',
        component: () => import('@/views/reconciliation/Results.vue'),
        meta: { title: '对账结果' }
      },
      {
        path: 'manual-match',
        name: 'ReconciliationManualMatch',
        component: () => import('@/views/reconciliation/ManualMatch.vue'),
        meta: { title: '手工匹配' }
      }
    ]
  },
  
  // 销售管理
  {
    path: '/sales',
    component: Layout,
    meta: { title: '销售管理', icon: 'TrendCharts' },
    children: [
      {
        path: 'dashboard',
        name: 'SalesDashboard',
        component: () => import('@/views/sales/SalesDashboard.vue'),
        meta: { title: '销售仪表板' }
      },
      {
        path: 'customers',
        name: 'SalesCustomers',
        component: () => import('@/views/sales/Customers.vue'),
        meta: { title: '客户管理' }
      },
      {
        path: 'orders',
        name: 'SalesOrders',
        component: () => import('@/views/sales/SalesOrders.vue'),
        meta: { title: '销售订单' }
      },
      {
        path: 'analysis',
        name: 'SalesAnalysis',
        component: () => import('@/views/sales/Analysis.vue'),
        meta: { title: '销售分析' }
      }
    ]
  },
  
  // 库存管理
  {
    path: '/inventory',
    component: Layout,
    meta: { title: '库存管理', icon: 'Box' },
    children: [
      {
        path: 'products',
        name: 'InventoryProducts',
        component: () => import('@/views/inventory/Products.vue'),
        meta: { title: '产品管理' }
      },
      {
        path: 'stock',
        name: 'InventoryStock',
        component: () => import('@/views/inventory/Stock.vue'),
        meta: { title: '库存管理' }
      }
    ]
  },
  
  // 采购管理
  {
    path: '/purchase',
    component: Layout,
    meta: { title: '采购管理', icon: 'ShoppingCart' },
    children: [
      {
        path: 'suppliers',
        name: 'PurchaseSuppliers',
        component: () => import('@/views/purchase/Suppliers.vue'),
        meta: { title: '供应商管理' }
      },
      {
        path: 'orders',
        name: 'PurchaseOrders',
        component: () => import('@/views/purchase/PurchaseOrders.vue'),
        meta: { title: '采购订单' }
      }
    ]
  },
  
  // 系统管理
  {
    path: '/system',
    component: Layout,
    meta: { title: '系统管理', icon: 'Setting' },
    children: [
      {
        path: 'users',
        name: 'SystemUsers',
        component: () => import('@/views/system/Users.vue'),
        meta: { title: '用户管理' }
      },
      {
        path: 'roles',
        name: 'SystemRoles',
        component: () => import('@/views/system/RolePermissions.vue'),
        meta: { title: '角色管理' }
      },
      {
        path: 'settings',
        name: 'SystemSettings',
        component: () => import('@/views/system/SystemSettings.vue'),
        meta: { title: '系统设置' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router